<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  img {
    width: 600px;
    height: 800px;
  }
</style>

<body>
  <div>
    <img src="" data-src="./image/nature-1.jpg" alt="">
    <img src="" data-src="./image/nature-2.jpg" alt="">
    <img src="" data-src="./image/nature-3.jpg" alt="">
    <img src="" data-src="./image/nature-4.jpg" alt="">
    <img src="" data-src="./image/nature-5.jpg" alt="">
    <img src="" data-src="./image/nature-6.jpg" alt="">
  </div>
  <script>
    const images = document.querySelectorAll('img');
    const callback = (entries) => {
      entries.forEach(entry => {
        console.log(entry.isIntersecting)
        if (entry.isIntersecting) {            // 监听到出现
          const image = entry.target           // 获取目标
          image.setAttribute('src', image.getAttribute('data-src'))
          observer.unobserve(image)            // 取消监听
          console.log('触发');
        }
      })
    }
    const observer = new IntersectionObserver(callback)
    images.forEach(image => {
      observer.observe(image)
    })
  </script>
</body>

</html>